package cn.kgc.vue.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Score;
import cn.kgc.vue.exceptions.StudentNoException;
import cn.kgc.vue.mapper.ScoreMapper;
import cn.kgc.vue.service.ScoreService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
* @author mengshun
* @description 针对表【sys_score】的数据库操作Service实现
* @createDate 2023-12-15 08:42:12
*/
@Service
@Transactional
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score> implements ScoreService{


    @Override
    public ResponseResult getScoreList(QueryCondition<Score> queryCondition) {


        Page<Score> scorePage = new Page<>(queryCondition.getPage(), queryCondition.getLimit());

        String clazz = queryCondition.getQuery().getClazz();
        Date examTime = queryCondition.getQuery().getExamTime();


        LambdaQueryWrapper<Score> lambda = new QueryWrapper<Score>().lambda();
        lambda.eq(StringUtils.isNotEmpty(clazz),Score::getClazz,clazz)
                .eq(ObjectUtils.isNotNull(examTime),Score::getExamTime,examTime)
                .orderByDesc(Score::getExamTime);

        baseMapper.selectPage(scorePage,lambda);

        HashMap<String, Object> result = new HashMap<>();
        result.put("total",scorePage.getTotal());
        result.put("pageData",scorePage.getRecords());

        return ResponseResult.success().data(result);
    }

    @Override
    public ResponseResult batchImportScore(MultipartFile file) {

        if (file.isEmpty()){
            ResponseResult.fail().message("请选择文件");
        }


        try {
            InputStream inputStream = file.getInputStream();
            ImportParams importParams = new ImportParams();
            importParams.setHeadRows(1);
            importParams.setTitleRows(1);

            List<Score> scores = ExcelImportUtil.importExcel(inputStream, Score.class, importParams);

            scores.forEach(score -> {
                LambdaQueryWrapper<Score> lambda = new QueryWrapper<Score>().lambda();
                lambda.eq(Score::getStudentNo,score.getStudentNo())
                        .eq(Score::getExamTime,score.getExamTime());
                Score one = baseMapper.selectOne(lambda);
                if (ObjectUtils.isNotNull(one)){
                    throw new StudentNoException();
                }
                baseMapper.insert(score);
            });

            return ResponseResult.success().message("数据导入成功");

        }catch (StudentNoException e){
            throw new StudentNoException();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult screenData() {

        List<Score>  datas  =  baseMapper.selectScreenData();

        return ResponseResult.success().data(datas);
    }
}




